// ColorTunning.cpp : implementation file
//

#include "stdafx.h"
#include "yujinsrhost.h"
#include "ColorTunning.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorTunning dialog


CColorTunning::CColorTunning(CWnd* pParent /*=NULL*/)
	: CDialog(CColorTunning::IDD, pParent)
{
	//{{AFX_DATA_INIT(CColorTunning)
	m_nHue = 0;
	m_nSaturation = 0;
	m_nContrast = 0;
	m_nBrightness = 0;	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CColorTunning::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CColorTunning)
	DDX_Control(pDX, IDC_HUE_SCROLLBAR, m_sHue);
	DDX_Control(pDX, IDC_SATURATION_SCROLLBAR, m_sSaturation);
	DDX_Control(pDX, IDC_CONTRAST_SCROLLBAR, m_sContrast);
	DDX_Control(pDX, IDC_BRIGHT_SCROLLBAR, m_sBrightness);
	DDX_Text(pDX, IDC_EDIT1, m_nHue);
	DDX_Text(pDX, IDC_EDIT2, m_nSaturation);
	DDX_Text(pDX, IDC_EDIT3, m_nContrast);
	DDX_Text(pDX, IDC_EDIT4, m_nBrightness);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CColorTunning, CDialog)
	//{{AFX_MSG_MAP(CColorTunning)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorTunning message handlers

void CColorTunning::OnButton1() 
{
	
	m_nHue = 128;
	m_nSaturation = 128;
	m_nContrast = 128;
	m_nBrightness = 128;
	MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_HUE_REF,  m_nHue);
 	MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_SATURATION_REF, m_nSaturation);
	MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_CONTRAST_REF,  m_nContrast);
    MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_BRIGHTNESS_REF, m_nBrightness);
 
	m_sHue.SetScrollPos(m_nHue);
	m_sContrast.SetScrollPos(m_nContrast);
	m_sSaturation.SetScrollPos(m_nSaturation);
	m_sBrightness.SetScrollPos(m_nBrightness);

	UpdateData(FALSE);

}

BOOL CColorTunning::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_sHue.SetScrollRange(0,255);
	m_sContrast.SetScrollRange(0,255);
	m_sSaturation.SetScrollRange(0,255);
	m_sBrightness.SetScrollRange(0,255);

	m_sHue.SetScrollPos(m_nHue);
	m_sContrast.SetScrollPos(m_nContrast);
	m_sSaturation.SetScrollPos(m_nSaturation);
	m_sBrightness.SetScrollPos(m_nBrightness);

	m_Hue_orginal = m_nHue;
	m_Contrast_orginal = m_nContrast;
	m_Saturation_orginal = m_nSaturation;
	m_Brightness_orginal = m_nBrightness;

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CColorTunning::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	int pos,nID,index;
    nID	= GetWindowLong(pScrollBar->m_hWnd, GWL_ID);		
	index =nID-IDC_HUE_SCROLLBAR;
	pos = pScrollBar->GetScrollPos();
    switch(nSBCode){
	case  SB_LEFT :
			pos = 0;
			break;
	case  SB_RIGHT :
			pos = 255;
			break;
    case  SB_LINELEFT:
			pos=pos-1;
			break;
	case  SB_LINERIGHT:
			pos=pos+1;
			break;
    case  SB_PAGELEFT:
            pos = pos-5;
			break;
	case  SB_PAGERIGHT:		
			pos = pos +5;
			break;
	case  SB_THUMBPOSITION:
			pos = nPos;
			break;
	case  SB_THUMBTRACK:
			break;
	}
	if (pos < 0 ) pos = 0;
	if (pos >255) pos = 255;
	pScrollBar->SetScrollPos(pos);
    switch(index){
	case 0 :
		m_nHue = pos;
		break;
	case 1 :
		m_nSaturation = pos;
		break;
	case 2 :
		m_nContrast = pos;
		break;
	case 3 : 
	    m_nBrightness = pos; 
	}
	
	MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_HUE_REF,  m_nHue);
 	MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_SATURATION_REF, m_nSaturation);
	MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_CONTRAST_REF,  m_nContrast);
    MdigReference(((CMainFrame*)AfxGetMainWnd())->MilDigitizer, M_BRIGHTNESS_REF, m_nBrightness);
 	

	UpdateData(FALSE); 

	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
